﻿layui.use(['form', 'table', 'layer', 'element'], function () {
    var form = layui.form;
    var table = layui.table;
    var layer = layui.layer;
    var element = layui.element;
    var $ = layui.jquery;
    getInfo();
    function getInfo() {
        var index = layer.load();
        $.getData('api/Role/GetAllPower', (data) => {
            if (data.code == API_RES_CODE.SUCCESS) {
                layui.layer.close(index);
                loadTable(data.data);
            } else {
                layer.msg(data.msg);
            }
        })

    }

    function loadTable(jsonData) {
        table.render({
            elem: '#paramTable'
            , data: jsonData
            , cols: [[
                { field: 'departName', title: '部门', align: 'center' }
                , { field: 'card', title: '卡号', align: 'center' }
                , { field: 'name', title: '姓名', align: 'center' }
                , { field: 'roleName', title: '角色名', align: 'center' }
                , { field: 'role', title: '角色列表', align: 'center', hide: true }
                , { align: 'center', title: '操作', toolbar: '#barDemo', width: "10%" }

            ]]
            , page: true
            , limit: 90
        });
    }

    form.on('submit(Add)', function (data) {
        var dialogHeight = window.top.document.getElementById('LAY_app_body').scrollHeight * 0.95;
        var dialogWidth = document.body.scrollWidth * 1;
        layer.open({
            type: 2
            , title: '赋予角色'
            , content: 'powerAdd.html'
            , maxmin: true
            , area: [dialogWidth + 'px', dialogHeight + 'px']
        });
    });

    table.on('tool(paramTable)', function (obj) {
        var data = obj.data;
        var dataList = [];
        dataList.push(data);
        var dialogHeitht = window.top.document.getElementById('LAY_app_body').scrollHeight * 0.95;
        var dialogWidth = document.body.scrollWidth * 0.8;
        // indexRow =  obj.tr[0].rowIndex;
        indexCol = $(this).closest('td').data('field');
        if (obj.event === 'info') {
            layer.open({
                type: 2
                , title: '基础信息'
                , content: 'RoleCreate.html?Type=' + 1 + "&Card=" + data.role
                , maxmin: true
                , area: [dialogWidth + 'px', dialogHeitht + 'px']
            });
        } else if (obj.event === 'del') {
            layer.confirm('确认删除该管理员？', {
                btn: ['确认', '取消'] //按钮
            }, function (index) {
                deleteParam(data.id, index);
            }, function () {
            });
        }
    });

    //监听查询按钮的点击事件
    form.on('submit(sub)', function (data) {
        ajaxGetAll();
    });

    //按钮组对应功能
    var $ = layui.$, active = {

    };

    //点击按钮进行对应操作
    $('.test-table-operate-btn .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    $(document).off('mousedown', '.layui-table-grid-down').on('mousedown', '.layui-table-grid-down', function (event) {
        //直接记录td的jquery对象
        table._tableTrCurrr = $(this).closest('td');
    });

    //给弹出的详情里面的按钮添加监听级联的触发原始table的按钮的点击事件
    $(document).off('click', '.layui-table-tips-main [lay-event]').on('click', '.layui-table-tips-main [lay-event]', function (event) {
        var elem = $(this);
        var tableTrCurrr = table._tableTrCurrr;
        if (!tableTrCurrr) {
            return;
        }
        var layerIndex = elem.closest('.layui-table-tips').attr('times');
        layer.close(layerIndex);
        table._tableTrCurrr.find('[lay-event="' + elem.attr('lay-event') + '"]').first().click();
    });
    //点击弹出事件
    function openUserSelect() {
        layer.open({
            type: 2,
            title: false,
            shadeClose: true,
            shade: [0],
            scrollbar: false,
            maxmin: false, //开启最大化最小化按钮
            area: ['790px', '640px'],
            content: '../SystemManage/userSelect.html',
            offset: '25px',
            zindex: 19891099,
        })
    }

    // 只获取数据
    function ajaxGetAll() {
        var index = layer.load();
        $.postJSON('api/Role/getPowerInfoByParam', { Card: $("#title").val(), Name: $("#title2").val() }, (data) => {
            layui.layer.close(index);
            if (data.code == API_RES_CODE.SUCCESS) {
                loadTable(data.data);
            } else {
                layer.alert(data.msg);
            }
        })
    }

    function deleteParam(id, index) {
        $.postJSON("/api/Role/deletePower", { ID: id }, (data) => {
            if (data.code == API_RES_CODE.SUCCESS) {
                if (data.msg === "CanNotDe") {
                    layer.alert("删除失败,超级管理员至少有一个");
                } else {
                    layer.alert("删除成功");
                    layer.close(index);
                    getInfo();
                }

            } else {
                layer.alert(data.msg);
            }
        })
    }
});

